package com.wjc.myktapplication.page.search

import android.graphics.Color
import android.os.Bundle
import android.text.SpannableString
import android.text.Spanned
import android.text.TextPaint
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.text.style.ForegroundColorSpan
import android.view.View
import android.widget.Toast
import com.wjc.basemodule.base.BaseFragment
import com.wjc.basemodule.base.DataBindingConfig
import com.wjc.myktapplication.R
import com.wjc.myktapplication.databinding.FragmentSearchBinding


class SearchFragment : BaseFragment<FragmentSearchBinding>() {
    override val dataBindingConfig: DataBindingConfig
        get() = DataBindingConfig(R.layout.fragment_search)


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
//        setHasDarkMode(true)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
//        mBinding.ivTop.setOnClickListener {
//            nav().navigateUp()
//        }
        createSpannableString()
    }

    private var checked = false
    private fun createSpannableString() {
        val tips01 = "同意"
        val tips02 = "《用户协议》"
        val tips03 = "《隐私政策》"
        val tips04 = "《儿童隐私政策》"

        SpannableString(tips01 + tips02 + tips03 + tips04).also {
            it.setSpan(
                ForegroundColorSpan(Color.parseColor("#CD11CD")),
                0,
                tips01.length ,
                Spanned.SPAN_EXCLUSIVE_INCLUSIVE
            )

            it.setSpan(object : ClickableSpan() {
                override fun onClick(widget: View) {
                    Toast.makeText(context, "点击用户协议", Toast.LENGTH_LONG).show()
                }

                override fun updateDrawState(ds: TextPaint) {
                    ds.color = Color.WHITE
                    ds.isUnderlineText = false
                }
            }, tips01.length, tips01.length + tips02.length, Spanned.SPAN_INCLUSIVE_INCLUSIVE)

            it.setSpan(object : ClickableSpan() {
                override fun onClick(widget: View) {
                    Toast.makeText(context, "点击隐私政策", Toast.LENGTH_LONG).show()
                }

                override fun updateDrawState(ds: TextPaint) {
                    ds.color = Color.RED
                    ds.isUnderlineText = false
                }
            },
                (tips01 + tips02).length,
                (tips01 + tips02 + tips03).length,
                Spanned.SPAN_INCLUSIVE_INCLUSIVE
            )

            it.setSpan(object : ClickableSpan() {
                override fun onClick(widget: View) {
                    Toast.makeText(
                        context, "点击儿童用户协议",
                        Toast.LENGTH_LONG
                    ).show()
                }

                override fun updateDrawState(ds: TextPaint) {
                    ds.color = Color.WHITE
                    ds.isUnderlineText = false
                }
            },  (tips01 + tips02 + tips03).length, it.length, Spanned.SPAN_INCLUSIVE_INCLUSIVE)
            binding.tvSpan.apply {
//                movementMethod = LinkMovementMethod.getInstance()
//                highlightColor = Color.TRANSPARENT
                setOnClickListener {
                    if (selectionStart == -1 && selectionEnd == -1) {
                        checked = !checked
                        if (checked) {
                            setCompoundDrawablesWithIntrinsicBounds(
                                resources.getDrawable(R.mipmap.ic_logged_out_selected),
                                null,
                                null,
                                null
                            )
                        } else {
                            setCompoundDrawablesWithIntrinsicBounds(
                                resources.getDrawable(R.mipmap.ic_logged_out_normal),
                                null,
                                null,
                                null
                            )
                        }
                    }
                }
            }
        }
    }
}